package com.atguigu.crowd.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.po.MemberPO;
import com.atguigu.crowd.service.api.MemberProviderService;
import com.atguigu.crowd.util.ResultEntity;

@RestController
public class MemberProviderHandler {

	@Autowired
	private MemberProviderService memberProviderService;
	
	//保存注册信息
	@RequestMapping("/save/member/remote")
	public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO){
		try {
			memberProviderService.saveMember(memberPO);
			return ResultEntity.successWithoutData();
			
		} catch (Exception e) {
			if (e instanceof DuplicateKeyException) {//唯一约束
				return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
			}
			return ResultEntity.failed(e.getMessage());
		}
	}
	//根据账号获取会员
	@RequestMapping("/get/memberpo/by/loginacct/remote")
	ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginAcct") String loginAcct){
		try {
			MemberPO memberPO = memberProviderService.getMemberPOByLoginAcct(loginAcct);
			return ResultEntity.successWithData(memberPO);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultEntity.failed(e.getMessage());
		}
	}
	
}
